package com.auxgroup.smarthome.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 构建树对象
 * @Author: laiqiuhua.
 * @Date: 2017/10/23 13:59.
 */
public class TreeNode {
    private String pid;
    private String id;
    private String name;
    private TreeNode parentNode;
    private String rootId;
    private boolean isLeaf;
    private int level;
    private int sort;
    private Object object; // 自定义对象
    private List<TreeNode> children = new ArrayList<>();

    public TreeNode(){}

    public TreeNode(String pid, String id, Object object) {
        this.pid = pid;
        this.id = id;
        this.object = object;
    }

    public TreeNode(String pid, String id, String name, Object object, List<TreeNode> children) {
        this.pid = pid;
        this.id = id;
        this.name = name;
        this.object = object;
        this.children = children;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    @JsonIgnore
    public TreeNode getParentNode() {
        return parentNode;
    }

    public void setParentNode(TreeNode parentNode) {
        this.parentNode = parentNode;
    }

    public String getRootId() {
        return rootId;
    }

    public void setRootId(String rootId) {
        this.rootId = rootId;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    public boolean isLeaf() {
        return isLeaf;
    }

    public void setLeaf(boolean leaf) {
        isLeaf = leaf;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TreeNode treeNode = (TreeNode) o;
        return Objects.equals(pid, treeNode.pid) &&
                Objects.equals(id, treeNode.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pid, id);
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("TreeNode{");
        sb.append("pid='").append(pid).append('\'');
        sb.append(", id='").append(id).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", rootId='").append(rootId).append('\'');
        sb.append(", isLeaf=").append(isLeaf);
        sb.append(", level=").append(level);
        sb.append(", sort=").append(sort);
        sb.append(", children=").append(children);
        sb.append('}');
        return sb.toString();
    }

}
